package com.sec.etech.org.service;

import org.openbpm.base.api.Page;
import org.openbpm.base.api.query.QueryFilter;
import org.openbpm.base.api.query.QueryOP;
import org.openbpm.base.core.cache.ICache;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.base.db.model.query.DefaultPage;
import org.openbpm.base.db.model.query.DefaultQueryFilter;
import org.openbpm.org.core.dao.ResRoleDao;
import org.openbpm.org.core.manager.ResRoleManager;
import org.openbpm.org.core.manager.impl.ResRoleManagerImpl;
import org.openbpm.org.core.model.ResRole;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Primary
@Service("etechResRoleManager")
public class EtechResRoleManager extends ResRoleManagerImpl implements ResRoleManager {

    @Resource
    ResRoleDao resRoleDao;

    @Resource
    ICache iCache;
    public final String URL_ROLE_MAPPING = "openbpm:sys:resoucesUrlRoleMapping:";

    @Override
    public void assignResByRoleSys(String resIds, String systemId, String roleId) {
        QueryFilter queryFilter = new DefaultQueryFilter();
        queryFilter.addFilter("system_id_", systemId, QueryOP.EQUAL);
        queryFilter.addFilter("role_id_", roleId, QueryOP.EQUAL);
        DefaultPage page = new DefaultPage();
        page.setLimit(10000);
        queryFilter.setPage(page);
        List<ResRole> oldResList = resRoleDao.query(queryFilter);
        List<String> delIds = new ArrayList<>();
        Map<String, String> oldRes2id = new HashMap<>();
        for(ResRole resRole: oldResList){
            if(!systemId.equals(resRole.getSystemId())){
                continue;
            }
            delIds.add(resRole.getId());
            oldRes2id.put(resRole.getResId(), resRole.getId());
        }
        if(StringUtil.isNotEmpty(resIds)){
            String[] aryRes = resIds.split(",");
            for (String resId : aryRes) {
                resId = resId.trim();
                if ("".equals(resId)|| "0".equals(resId)) {
                    continue;
                }
                if(oldRes2id.containsKey(resId)){
                    // remains
                    delIds.remove(oldRes2id.get(resId));
                    oldRes2id.remove(resId);
                }else {
                    // new
                    ResRole resRole = new ResRole(systemId,resId,roleId);
                    resRoleDao.create(resRole);
                }
            }
        }
        // del
        for(String id: delIds){
            resRoleDao.remove(id);
        }
        cleanResoucesCache();
    }

    public void assignResByRoleSys2(String resIds, String systemId, String roleId) {
        resRoleDao.removeByRoleAndSystem(roleId, systemId);
        // fix resIds为空插入空记录
        if(StringUtil.isNotEmpty(resIds)){
            String[] aryRes = resIds.split(",");
            for (String resId : aryRes) {
                resId = resId.trim();
                if ("".equals(resId)|| "0".equals(resId)) {
                    continue;
                }
                ResRole resRole = new ResRole(systemId,resId,roleId);
                resRoleDao.create(resRole);
            }
        }
        cleanResoucesCache();
    }

    private void cleanResoucesCache() {
        iCache.delByKey(URL_ROLE_MAPPING);
    }
}
